// mircoApp/store_screen/form/form.js
import Dialog from "../../../dist/dialog/dialog";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tips_index:"",
    leave_class: true,
    title: "",
    project_name: '选择项目',
    show_tips: false,
    show: false,
    id: "",
    list: [{
      name: "",
      price: '',
      project_id: "",
      unit_name:"",
      is_class:true,
      special_sign:0
    }],
    tips_list: [],
    current_index: "",
    store_id: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id || ''
    });
    this.data.store_id = options.store_id || '';
    if (options.id) {
      wx.setNavigationBarTitle({
        title: '编辑成本',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '添加成本',
      })
    }
    this.init();
    this.get_project_data();
    if (options.id) {
      this.getDetail();
    }
  },
  close_animation(){
    this.setData({
      leave_class: true
    });
    setTimeout(() => {
      this.setData({
        show_tips: false
      });
    }, 500);
  },
  hideDropdownIfOutside(event) {
    if (!event.currentTarget.dataset.item) {
      this.close_animation();
    }
  },
  getDetail() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index.details"),
      data: {
        id: this.data.id,
        store_id: this.data.store_id
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          this.data.list = [{
            name: data.project_name,
            price: data.real_price,
            project_id: data.project_id,
            id: data.id,
            unit_name:data.project.unit_name,
            special_sign:data.project.special_sign
          }];
          this.setData({
            list: this.data.list
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  submit() {
    let store_id = this.data.store_id || '';
    let json = {
      data: this.data.list.map(item => {
        let _json = {
          price:item.price,
          project_id:item.project_id,
          special_sign:item.special_sign
        }
        if(item.id){
            _json.id = item.id;
        }
        return _json;
      })
    };
    if (store_id) {
      json.store_id = store_id;
    }

    app._postNetWork({
      url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index.save"),
      data: json,
      success: resdata => {
        let {
          result,
          msg
        } = resdata.data;
        app.tips(msg);
        if (result) {
          setTimeout(() => {
            wx.navigateBack();
          }, 400);
        }
        console.log(this.data.list,'借宿后欧撒鸥哦所欧撒鸥哦所');
      }
    })
  },
  handle_add() {
     this.data.list.push({
      name: "",
      price: '',
      project_id: "",
      unit_name:"",
      is_class:true,
      special_sign:0
    });
    this.setData({ list: this.data.list});
    setTimeout(() => {
      this.data.list[this.data.list.length-1].is_class=false;
      this.setData({ list: this.data.list});
    }, 1000);
  },
  get_project_data() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.store-screen.frontend.controllers.cost.index.get-project-list"),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          this.setData({
            tips_list: data
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  startTouch(e) {
    let divColor = this.data.divColor === 'white' ? '#F5F5F5' : 'white';
    this.setData({
      current_elem_id: e.currentTarget.dataset.item.id,
      divColor
    });
  },
  endTouch() {
    this.setData({
      divColor: "white"
    })
  },
  handle_click(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      tips_index: index
    });
    if (!this.data.show_tips) {
      this.setData({
        show_tips: true,
        leave_class: false
      });
    } else {
      this.close_animation();
    }
  },
  init() {
    if (this.data.id) {
      this.data.title = '编辑成本'
    } else {
      this.data.title = '添加成本';
    }
    this.setData({
      title: this.data.title,
      show: false
    });

  },
  handle_delete(e) {
    let { index,item} = e.currentTarget.dataset;
    console.log(item,'item===');
    Dialog.confirm({
        title: '提示',
        message: '删除后无法恢复，确认删除吗？',
      })
      .then(() => {
        this.setData({
          current_index: index
        });
        setTimeout(() => {
          this.data.list.splice(index, 1);
          this.setData({
            current_index:'',
            list: this.data.list
          });
        }, 1000);
      })
      .catch(() => {});
  },
  hanlde_input(e) {
    let index = e.currentTarget.dataset.index;
    this.data.list[index].price = e.detail.value;
    this.data.list[index].is_class = false;
    this.setData({
      list: this.data.list
    })
  },
  choose(e) {
    let {
      item,
      index
    } = e.currentTarget.dataset;
    this.data.list[index].name = item.name;
    this.data.list[index].project_id = item.id;
    this.data.list[index].unit_name = item.unit_name;
    this.data.list[index].special_sign = item.special_sign
    this.setData({
      list: this.data.list
    });
    this.close_animation();
  },
  onConfirm(date) {
    this.show = false;
    this.currentDate = date;
    if (this.showType == 1) {
      this.startTime = date;
      this.form.id_card_start_day = this.fun.dateFormat("YYYYmmdd", date);
    } else {
      this.endTime = date;
      this.form.id_card_end_day = this.fun.dateFormat("YYYYmmdd", date);
    }
  },
  cancel() {
    this.show = false;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})